package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import messages.SystemMessage;
import controlleur.Controlleur;
import controlleur.SuccursaleControlleur;

/**
 * Servlet implementation class SuccursaleServlet
 */
public class SuccursaleServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	Controlleur ctrl = new SuccursaleControlleur();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SuccursaleServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		final String action = request.getParameter(SystemMessage.ACTION);
		PrintWriter out = response.getWriter();
		if (action!= null)
		{
			if(action.equals(SystemMessage.GET_ALL_SUCCUR))
			{
				int idDetaillant = Integer.parseInt(request.getParameter(SystemMessage.DETAILLANT_ID));
				out.print(((SuccursaleControlleur) ctrl).getAllSuccFromDet(idDetaillant));
			}
			else if(action.equals(SystemMessage.GET_SUCCUR))
			{
				int idSuccur = Integer.parseInt(request.getParameter(SystemMessage.SUCCURSALE_ID));
				out.print(((SuccursaleControlleur) ctrl).getSucc(idSuccur));
			}
			else if(action.equals(SystemMessage.ADD_NEW_SUCCUR))
			{
				//int succId = Integer.parseInt(request.getParameter(SystemMessage.SUCCURSALE_ID));
				int detId = Integer.parseInt(request.getParameter(SystemMessage.DETAILLANT_ID));
				String succNom=request.getParameter(SystemMessage.SUCCURSALE_NOM);
				String succAdr=request.getParameter(SystemMessage.SUCCURSALE_ADDRES);
				String succCP=request.getParameter(SystemMessage.SUCCURSALE_CP);				
				String succDesc=request.getParameter(SystemMessage.SUCCURSALE_DESC);
				
				((SuccursaleControlleur) ctrl).addSucc(succNom,succAdr,succCP,succDesc,detId);
			}
			else if(action.equals(SystemMessage.DEL_SUCCUR))
			{
				int idSuccur = Integer.parseInt(request.getParameter(SystemMessage.SUCCURSALE_ID));
				((SuccursaleControlleur) ctrl).delSucc(idSuccur);
			}
			else if(action.equals(SystemMessage.UPD_SUCCUR))
			{
				String succNom=request.getParameter(SystemMessage.SUCCURSALE_NOM);
				String succAdr=request.getParameter(SystemMessage.SUCCURSALE_ADDRES);
				String succCP=request.getParameter(SystemMessage.SUCCURSALE_CP);
				int succLon=Integer.parseInt(request.getParameter(SystemMessage.SUCCURSALE_LON));
				int succLat=Integer.parseInt(request.getParameter(SystemMessage.SUCCURSALE_LAT));
				String succDesc=request.getParameter(SystemMessage.SUCCURSALE_DESC);
				int id = Integer.parseInt(request.getParameter(SystemMessage.SUCCURSALE_ID));
				
				((SuccursaleControlleur) ctrl).updSucc(id, succNom,succAdr,succCP,succLon,succLat,succDesc);
			}
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		final String action = request.getParameter(SystemMessage.ACTION);
		PrintWriter out = response.getWriter();
		if (action!= null)
		{

			/*if(action.equals(SystemMessage.GET_ALL_SUCCUR))
			{
				
			}
			else if(action.equals(SystemMessage.GET_SUCCUR))
			{
				
			}
			else if(action.equals(SystemMessage.ADD_NEW_SUCCUR))
			{
				
			}
			else if(action.equals(SystemMessage.DEL_SUCCUR))
			{
				
			}
			else if(action.equals(SystemMessage.UPD_SUCCUR))
			{
				
			}*/
		}
	}

}
